<?php
namespace BoxSns\Logic;
class CommentLogic extends \LogicController {
	public function _initialize() {
		parent::_initialize();
		// $this->SnsComment=BoxModel("sns_comment");
		//$this->user_id=$this->user_info['user_id'];
	}

	/*******评论*****/
	public function add($row) {
		$error = Logic("BoxSns", "Member")->check($this->user_id);
		if ($error['error'] > 0) {
			return $error;
		}
		/****是否定义可以发此类型的评论**/
		$content = $row['content'];
		$content = stripslashes(htmlspecialchars_decode($content));
		if (!trim(strip_tags($content, "img"))) {
			return array('error' => 3, 'msg' => '提交内容不能为空');
		}
		$content = strip_tags($content, dict('allow_tags')); //过滤html标签
		//$content = autolink($content);                    //加上超链接
		$content = AtStr($content); //有
		$row['content'] = $content;
		$row['user_id'] = $this->user_id;
		if ($row['to_comment_id']) {
//回复
			$toCommentData = BoxModel("sns_comment")->where(array('id' => $row['to_comment_id']))->find();
			$row['to_user_id'] = $toCommentData['user_id'];
		}
		$comment_id = BoxModel("sns_comment")->create_comment($row);
		if ($comment_id) {
			$msg = BoxModel("sns_comment")->load($comment_id);
		} else {
			$msg = "系统错误";
		}
		return array(
			'error' => 0,
			'msg' => $msg,
			//'data'  =>BoxModel("sns_comment")->load($comment_id),
		);
	}

	/*****删除帖
		     * $param=array(
		     * 'type'=>$type,
		     * 'comment_id'=>$comment_id,
		     * );
	*/
	public function del($comment_id) {
		$r = $this->act_del($comment_id);
		if ($r['error'] > 0) {
			return $r;
		}
		$bool = BoxModel("sns_comment")->del($comment_id);
		if ($bool) {
			$r = array(
				'error' => 0,
				'msg' => '删除成功',
			);
		} else {
			$r = array(
				'error' => 1,
				'msg' => '系统错误',
			);
		}
		return $r;
	}

	public function act_del($comment_id) {
		$data = BoxModel("sns_comment")->load($comment_id);
		if (!$data) {
			return array(
				'error' => 2,
				'msg' => '评论不存在',
			);
		}
		//dump($data);
		/*******版主和发帖人可以删除****/
		if ($data['pidData']['user_id'] != $this->user_id && $data['user_id'] != $this->user_id) {
			return array(
				'error' => 1,
				'msg' => '你无权操作',
			);
		}
		return true;
	}

	/*******回复列表****/
	public function get($param) {
		$topic_id = $param['topic_id'];
		$limit = $param['limit'];
		$type = $param['type'] ? $param['type'] : "group";
		$where = "pid='$topic_id' and type='$type'";
		$this->comment_count = 0;
		$data = BoxModel("SnsComment")->get($where, $limit, $param['orderBy']);
		foreach ($data['list'] as $k => $one) {
			if ($this->act_del($one['id']) === true) {
				$data['list'][$k]['del_act'] = 1;
			}
		}
		return $data;
	}

}